package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliyunOSSUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Tag(name = "文件上传")
@Validated
public class CommonController {
    
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        log.info("文件上传：{}", file.getOriginalFilename());
        String upload = aliyunOSSUtil.upload(file.getBytes(), Objects.requireNonNull(file.getOriginalFilename()));
        log.info("图片地址：{}", upload);
        if (StringUtils.isNoneBlank(upload)){
            return Result.success(upload,MessageConstant.UPLOAD_SUCCESS);
        }else {
            return Result.error(MessageConstant.UPLOAD_FAILED);
        }
    }
}
